var thisWidget;
function initWidgetView(t) {
    (thisWidget = t).config && thisWidget.config.style && $("body").addClass(thisWidget.config.style), $("#btn_plot_delall").click(function () {
        thisWidget.deleteAll()
    }), $("#btnDelete").click(function (t) {
        thisWidget.deleteCurrentEntity(), tab2plot()
    }), $("#btn_plot_savefile").click(function () {
        var t = thisWidget.arrFlyTable;
        null == t || 0 == t.length ? toastr.warning("当前没有漫游路线数据！") : haoutil.file.downloadFile("漫游路线数据.json", JSON.stringify(t))
    }), $("#btnSaveGeoJson").click(function () {
        var t = plotEdit._last_attr.attr.id;
        thisWidget.stopDraw();
        for (var a = 0, e = thisWidget.arrFlyTable.length; a < e; a++) {
            var i = thisWidget.arrFlyTable[a];
            if (i.id == t) {
                thisWidget.saveForGeoJson(i);
                break
            }
        }
    }), $("#btnSaveCzml").click(function () {
        var t = plotEdit._last_attr.attr.id;
        thisWidget.stopDraw();
        for (var a = 0, e = thisWidget.arrFlyTable.length; a < e; a++) {
            var i = thisWidget.arrFlyTable[a];
            if (i.id == t) {
                thisWidget.saveForCzml(i);
                break
            }
        }
    });
    var a = !0;
    $("#btn_plot_isedit").click(function () {
        (a = !a) ? ($(this).removeClass("active"), $(this).children().removeClass("fa-lock").addClass("fa-unlock")) : ($(this).addClass("active"), $(this).children().removeClass("fa-unlock").addClass("fa-lock")), thisWidget.hasEdit(a)
    }), plotEdit.initEvent(), plotEdit.loadConfig(), tableWork.initEvent()
}
var plotlist = {
    _listData: [{
        name: "线",
        type: "polyline",
        edittype: "polyline"
    }],
    startPlot: function (t, a) {
        var e = this._listData[t],
            i = e.edittype || e.type,
            l = haoutil.system.clone(plotEdit.defval[i] || {});
        if (e.style) for (var n in e.style) l.style[n] = e.style[n];
        if (e.attr) for (var n in e.attr) l.attr[n] = e.attr[n];
        l.type = e.type, l.name = l.name || e.name, thisWidget.startDraw(l)
    },
    plotEnd: function () {
        this._lastLi && this._lastLi.removeClass("markon")
    }
}, tableWork = {
    $table: null,
    getHeight: function () {
        return $(window).height() - 80
    },
    initEvent: function () {
        var l = this;
        $("#btn_Add_line").click(function () {
            plotlist.startPlot(0)
        }), $("#btnFlyStart").click(function (t) {
            var a = plotEdit._last_attr.attr.id;
            thisWidget.drawControl.stopDraw();
            for (var e = 0, i = thisWidget.arrFlyTable.length; e < i; e++) {
                var l = thisWidget.arrFlyTable[e];
                if (l.id == a) {
                    thisWidget.toRoamFly(l);
                    break
                }
            }
        });
        var t = $("#table");
        t.bootstrapTable({
            height: this.getHeight(),
            singleSelect: !0,
            pagination: !1,
            iconsPrefix: "fa",
            columns: [{
                field: "name",
                title: "名称",
                sortable: !0,
                editable: !1,
                align: "left"
            }, {
                field: "operate",
                title: "操作",
                align: "center",
                width: 80,
                events: {
                    "click .remove": function (t, a, e, i) {
                        thisWidget.deleteEntity(e.id)
                    },
                    "click .fly": function (t, a, e, i) {
                        thisWidget.toRoamFly(e)
                    },
                    "click .edit": function (t, a, e, i) {
                        l.showTableItem(e)
                    }
                },
                formatter: function (t, a, e) {
                    return ['<a class="fly" href="javascript:void(0)" title="飞行"><i class="fa fa-send-o"></i></a>&nbsp;&nbsp;', '<a class="edit" href="javascript:void(0)" title="编辑"><i class="fa fa-edit"></i></a>&nbsp;&nbsp;', '<a class="remove" href="javascript:void(0)" title="删除"><i class="fa fa-trash"></i></a>'].join("")
                }
            }],
            onDblClickRow: function (t, a, e) {
                l.showTableItem(t)
            }
        }), this.$table = t, this.loadData(thisWidget.arrFlyTable)
    },
    loadData: function (t) {
        this.$table.bootstrapTable("load", t)
    },
    showTableItem: function (t) {
        thisWidget.startEditingById(t.id)
    }
}, plotEdit = {
    hasEditSylte: !1,
    config: {},
    defval: {},
    initEvent: function () {
        var e = this;
        this.hasEditSylte || $("#attr_style_view").hide(), $("#plot_latlngs_allheight").bind("input propertychange", function () {
            $("#plot_latlngs_addheight").val("");
            var t = Number($(this).val());
            isNaN(t) && (t = 1);
            var a = [];
            $(".plot_latlngs").each(function () {
                "height" == $(this).attr("data-type") ? ($(this).val(t), a.push(t)) : a.push(Number($(this).val()))
            }), thisWidget.updateGeo2map(a, !0)
        }), $("#plot_latlngs_addheight").bind("input propertychange", function () {
            $("#plot_latlngs_allheight").val("");
            var a = Number($(this).val());
            isNaN(a) && (a = 1);
            var e = [];
            $(".plot_latlngs").each(function () {
                if ("height" == $(this).attr("data-type")) {
                    var t = Number($(this).attr("oldvalue"));
                    $(this).val(t + a), e.push(t + a)
                } else e.push(Number($(this).val()))
            }), thisWidget.updateGeo2map(e, !0)
        }), $("#txtFlySpeedAll").bind("input propertychange", function () {
            var t = Number($(this).val());
            if (0 < t) {
                var a = [];
                $(".plot_speeds").each(function () {
                    $(this).val(t), a.push(t)
                }), e._last_attr.speed = a
            }
        })
    },
    loadConfig: function () {
        var s = this;
        $.getJSON("config/attr.json", function (t) {
            for (var a in s.config = t) {
                for (var e = {}, i = 0; i < t[a].style.length; i++) {
                    e[(n = t[a].style[i]).name] = n.defval
                }
                var l = {};
                for (i = 0; i < t[a].attr.length; i++) {
                    var n;
                    l[(n = t[a].attr[i]).name] = n.defval
                }
                s.defval[a] = {
                    edittype: a,
                    name: t[a].name,
                    position: t[a].position,
                    style: e,
                    attr: l
                }
            }
        })
    },
    _last_attr: null,
    startEditing: function (t, a) {
        this._last_attr = t;
        var e = this.config[t.edittype] || {};
        a && this.updateLatlngsHtml(a, e.position);
        var i = [];
        if (this.hasEditSylte) {
            for (var l = "plot_attr_style_", n = '<tr><td class="nametd">类型：</td><td>' + (e.name || t.name) + "</td></tr>", s = 0; s < e.style.length; s++) {
                if ("hidden" != (o = e.style[s]).type) {
                    var r = o.name,
                        d = t.style[r];
                    (h = this.getAttrInput(l, r, d, o)).fun && i.push({
                        parname: l,
                        name: r,
                        value: d,
                        edit: o,
                        fun: h.fun
                    }), n += '<tr  id="' + l + "tr_" + r + '" > <td class="nametd">' + o.label + "</td>  <td>" + h.html + "</td>  </tr>"
                }
            }
            $("#talbe_style").html(n)
        }
        l = "plot_attr_attr_", n = "";
        for (s = 0; s < e.attr.length; s++) {
            var o;
            if ("hidden" != (o = e.attr[s]).type) {
                var h;
                r = o.name, d = t.attr[r];
                (h = this.getAttrInput(l, r, d, o)).fun && i.push({
                    parname: l,
                    name: r,
                    value: d,
                    edit: o,
                    fun: h.fun
                }), n += '<tr  id="' + l + "tr_" + r + '" > <td class="nametd">' + o.label + "</td>  <td>" + h.html + "</td>  </tr>"
            }
        }
        $("#talbe_attr").html(n);
        for (s = 0; s < i.length; s++) {
            var p = i[s];
            p.fun(p.parname, p.name, p.value, p.edit)
        }
        tab2attr()
    },
    lastCfg: null,
    updateLatlngsHtml: function (t, a) {
        a = a || this.lastCfg || {}, this.lastCfg = a, $("#plot_latlngs_addheight").val(""), $("#plot_latlngs_allheight").val(""), $("#view_updateheight").hide(), a.hasOwnProperty("height") || (a.height = !0);
        var e = "";
        if (t && 0 != t.length) if (1 == t.length) {
            var i = (h = t[0])[0],
                l = h[1],
                n = 3 == h.length ? h[2] : 0;
            e += ' <div class="mp_attr" style=" margin-top: 10px;"><table> <tr> <td class="nametd">经度：</td> <td><input type="number" class="mp_input plot_latlngs" data-type="jd" value="' + i + '"></td></tr><tr>  <td class="nametd">纬度：</td> <td><input type="number" class="mp_input plot_latlngs" data-type="wd" value="' + l + '"></td></tr>', a.height && (e += '<tr><td class="nametd">高程：</td> <td><input type="number" class="mp_input plot_latlngs" data-type="height" value="' + n + '" oldvalue="' + n + '"></td></tr>'), e += " </table> </div>"
        } else {
            a.height && $("#view_updateheight").show();
            var s = "";
            t.length > (a.minCount || 3) && (s = '<i class="fa fa-trash-o latlng-del" title="删除点" ></i>');
            for (var r = this._last_attr.speed, d = $("#txtFlySpeedAll").val(), o = 0; o < t.length; o++) {
                i = (h = t[o])[0], l = h[1], n = 3 == h.length ? h[2] : 0;
                var h, p = "";
                if ((null == a.maxCount || t.length < a.maxCount) && (p = '<i class="fa  fa-plus-square-o latlng-install" title="插入点" data-index="' + o + '" ></i>&nbsp;&nbsp;'), e += '<div><div class="open"><i class="tree_icon">-</i>第' + (o + 1) + '点 <label style="width:100px;">&nbsp;</label>    ' + p + s + ' </div><div class="mp_attr"><table><tr> <td class="nametd">经度：</td> <td><input  type="number" class="mp_input plot_latlngs" data-type="jd" data-index="' + o + '" value="' + i + '"></td>  </tr> <tr>  <td class="nametd">纬度：</td> <td><input  type="number" class="mp_input plot_latlngs" data-type="wd" data-index="' + o + '" value="' + l + '"></td> </tr> ', a.height && (e += '<tr>  <td class="nametd">高程：</td> <td><input  type="number" class="mp_input plot_latlngs" data-type="height" data-index="' + o + '" value="' + n + '" oldvalue="' + n + '"></td> </tr> '), o != t.length - 1) e += '<tr> <td class="nametd">航速(km/h)</td> <td><input  type="number" class="mp_input plot_speeds" title="第' + (o + 1) + "点至第" + (o + 2) + '点之间航速" data-type="speed" data-index="' + o + '" value="' + (r ? r[o] : d) + '"></td> </tr> ';
                e += " </table> </div> </div>"
            }
        } else;
        $("#view_latlngs").html(e), $("#view_latlngs .open").click(changeOpenShowHide);
        var c = this;
        $("#view_latlngs .latlng-del").click(function () {
            $(this).parent().parent().remove();
            var t = [],
                a = !1;
            $(".plot_latlngs").each(function () {
                t.push(Number($(this).val())), "height" === $(this).attr("data-type") && (a = !0)
            }), thisWidget.updateGeo2map(t, a);
            var e = [];
            if (a) for (var i = 0; i < t.length; i += 3) e.push([t[i], t[i + 1], t[i + 2]]);
            else for (i = 0; i < t.length; i += 2) e.push([t[i], t[i + 1]]);
            c.updateLatlngsHtml(e)
        }), $("#view_latlngs .latlng-install").click(function () {
            var t = Number($(this).attr("data-index")),
                a = [];
            $(".plot_speeds").each(function () {
                a.push(Number($(this).val()))
            }), a.splice(t + 1, 0, a[t]), c._last_attr.speed = a;
            var e = [],
                i = !1;
            $(".plot_latlngs").each(function () {
                e.push(Number($(this).val())), "height" === $(this).attr("data-type") && (i = !0)
            });
            var l = [];
            if (i) for (var n = 0; n < e.length; n += 3) l.push([e[n], e[n + 1], e[n + 2]]);
            else for (n = 0; n < e.length; n += 2) l.push([e[n], e[n + 1]]);
            var s = l[t],
                r = t == l.length - 1 ? l[0] : l[t + 1],
                d = Number(((s[0] + r[0]) / 2).toFixed(6)),
                o = Number(((s[1] + r[1]) / 2).toFixed(6));
            if (i) {
                var h = Number(((s[2] + r[2]) / 2).toFixed(1));
                l.splice(t + 1, 0, [d, o, h]), e.splice(3 * (t + 1), 0, d, o, h)
            } else l.splice(t + 1, 0, [d, o]), e.splice(3 * (t + 1), 0, d, o);
            c.updateLatlngsHtml(l), thisWidget.updateGeo2map(e, i)
        }), $(".plot_latlngs").bind("input propertychange", function () {
            var t = [],
                a = !1;
            $(".plot_latlngs").each(function () {
                t.push(Number($(this).val())), "height" === $(this).attr("data-type") && (a = !0)
            }), thisWidget.updateGeo2map(t, a)
        }), $(".plot_speeds").bind("input propertychange", function () {
            var t = [];
            $(".plot_speeds").each(function () {
                t.push(Number($(this).val()))
            }), c._last_attr.speed = t
        })
    },
    stopEditing: function () {
        tab2plot(), $("#talbe_style").html(""), $("#talbe_attr").html(""), this._last_attr = null
    },
    getAttrInput: function (t, a, e, i) {
        e = e || "";
        var n = this,
            l = "",
            s = null;
        switch (i.type) {
            default:
            case "label":
                l = e;
                break;
            case "text":
                l = '<input id="' + t + a + '" type="text" value="' + e + '"   class="mp_input" />', s = function (e, i, t, a) {
                    $("#" + e + i).on("input propertychange", function (t) {
                        var a = $(this).val();
                        n.updateAttr(e, i, a)
                    })
                };
                break;
            case "textarea":
                l = '<textarea  id="' + t + a + '"     class="mp_input" style="height:50px;resize: none;" >' + (e = e.replace(new RegExp("<br />", "gm"), "\n")) + "</textarea>", s = function (e, i, t, a) {
                    $("#" + e + i).on("input propertychange", function (t) {
                        var a = $(this).val();
                        0 == a.length && (a = "文字"), a = a.replace(/\n/g, "<br />"), n.updateAttr(e, i, a)
                    })
                };
                break;
            case "number":
                l = '<input id="' + t + a + '" type="number" value="' + e + '"    class="mp_input"/>', s = function (e, i, t, a) {
                    $("#" + e + i).on("input propertychange", function (t) {
                        var a = Number($(this).val());
                        n.updateAttr(e, i, a)
                    })
                };
                break;
            case "combobox":
                l = '<select id="' + t + a + '" class="mp_select"    data-value="' + e + '" >';
                for (var r = 0; r < i.data.length; r++) {
                    var d = i.data[r];
                    l += '<option value="' + d.value + '">' + d.text + "</option>"
                }
                l += "</select>", s = function (a, e, t, i) {
                    $("#" + a + e).select(), $("#" + a + e).change(function () {
                        var t = $(this).attr("data-value");
                        n.updateAttr(a, e, t), n.changeViewByAttrEx(a, e, t)
                    }), n.changeViewByAttrEx(a, e, t)
                };
                break;
            case "radio":
                var o = t + a;
                l = '  <div class="radio radio-info radio-inline">                                <input type="radio" id="' + o + '_1" value="1"  name="' + o + '"  ' + (e ? 'checked="checked"' : "") + '>                                <label for="' + o + '_1"> 是</label>                            </div>                            <div class="radio radio-info radio-inline">                                <input type="radio" id="' + o + '_2" value="2" name="' + o + '" ' + (e ? "" : 'checked="checked"') + ' ">                                <label for="' + o + '_2"> 否 </label>                            </div>', s = function (a, e, t, i) {
                    $('input:radio[name="' + a + e + '"]').change(function () {
                        var t = "1" == $(this).val();
                        n.updateAttr(a, e, t), n.changeViewByAttr(a, i.impact, t)
                    }), n.changeViewByAttr(a, i.impact, t)
                };
                break;
            case "color":
                l = '<input id="' + t + a + '"  class="mp_input" style="width: 100%;"  value="' + e + '" />', s = function (e, i, t, a) {
                    $("#" + e + i).minicolors({
                        position: "bottom right",
                        control: "saturation",
                        change: function (t, a) {
                            n.updateAttr(e, i, t)
                        }
                    })
                };
                break;
            case "slider":
                l = '<input id="' + t + a + '"  type="text" value="' + 100 * e + '" />', s = function (a, e, t, i) {
                    var l = .7 * $(".mp_tab_card").width() - 30;
                    $("#" + a + e).progress(l), $("#" + a + e).change(function () {
                        var t = Number($(this).val()) / 100;
                        n.updateAttr(a, e, t)
                    })
                };
                break;
            case "window":
                l = '<input id="' + t + a + '" type="text" value="' + e + '" readonly   class="mp_input" />', s = function (e, i, a, t) {
                    $("#" + e + i).on("click", function (t) {
                        thisWidget.showEditAttrWindow({
                            data: n._last_attr,
                            parname: e,
                            attrName: i,
                            attrVal: a
                        })
                    }), $("#" + e + i).on("input propertychange", function (t) {
                        var a = $(this).val();
                        n.updateAttr(e, i, a)
                    })
                }
        }
        return {
            html: l,
            fun: s
        }
    },
    changeViewByAttr: function (t, a, e) {
        if (a && 0 < a.length) for (var i = 0; i < a.length; i++) {
            var l = a[i];
            e ? $("#" + t + "tr_" + l).show() : $("#" + t + "tr_" + l).hide()
        }
    },
    changeViewByAttrEx: function (t, a, e) {
        if ("plot_attr_attr_" == t && "cameraType" == a) switch (e) {
            default:
            case "gs":
                $("#" + t + "tr_followedX").hide(),
                    $("#" + t + "tr_followedZ").hide();
                break;
            case "dy":
                $("#" + t + "tr_followedX").show(), $("#" + t + "tr_followedZ").show();
                break;
            case "sd":
                $("#" + t + "tr_followedX").hide(), $("#" + t + "tr_followedZ").show()
        }
    },
    updateAttr: function (t, a, e) {
        switch (t) {
            case "plot_attr_style_":
                this._last_attr.style[a] = e;
                break;
            case "plot_attr_attr_":
                this._last_attr.attr[a] = e
        }
        thisWidget.updateAttr2map(this._last_attr)
    }
};